package com.drawquest.network;

import com.drawquest.data.Comment;
import com.drawquest.data.Comments;
import com.drawquest.data.Quest;
import com.drawquest.data.User;
import com.gooddies.wiring.annotations.Wire;
import com.gooddies.wiring.annotations.WiringComponent;
import com.google.gson.JsonArray;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import java.util.Date;

/**
 * @author Dmitry
 */
@WiringComponent(singleton = true, lazy = true)
public class Network {

    @Wire
    private Downloader downloader;

    public Quest getCurrentQuest() {
        String res = downloader.download("https://api.drawquest.com/heavy_state_sync", Downloader.HttpMethod.POST);
        JsonObject mainElement = new JsonParser().parse(res).getAsJsonObject();
        JsonObject currentQuestObject = mainElement.get("current_quest").getAsJsonObject();
        Quest quest = parseQuest(currentQuestObject);
        return quest;
    }

    private Quest parseQuest(JsonObject questObject) {
        String title = questObject.get("title").getAsString();
        JsonObject contentObject = questObject.get("content").getAsJsonObject();
        String galleryUrl = contentObject.get("gallery").getAsJsonObject().get("url").getAsString();
        String editorTemplateUrl = contentObject.get("editor_template").getAsJsonObject().get("url").getAsString();
        String id = questObject.get("id").getAsString();
        Quest quest = new Quest(title, galleryUrl, editorTemplateUrl, id);
        return quest;
    }

    private Quest parseQuestFromComments(JsonObject questObject) {
        String title = questObject.get("quest_title").getAsString();
        String questId = questObject.get("quest_id").getAsString();
        JsonObject contentObject = questObject.get("content").getAsJsonObject();
        String galleryUrl = contentObject.get("iphone_gallery").getAsJsonObject().get("url").getAsString();
        String editorTemplateUrl = contentObject.get("editor_template").getAsJsonObject().get("url").getAsString();
        Quest quest = new Quest(title, galleryUrl, editorTemplateUrl, questId);
        return quest;
    }

    private User parseUser(JsonObject userObject) {
        User user = new User();
        user.setUserName(userObject.get("username").getAsString());
        user.setAvatarUrl(userObject.get("avatar_url").getAsString());
        return user;
    }

    public Comments getComments() {
        String res = downloader.download("https://api.drawquest.com/explore/comments", Downloader.HttpMethod.POST);
        JsonObject mainElement = new JsonParser().parse(res).getAsJsonObject();
        JsonArray commentsArray = mainElement.get("comments").getAsJsonArray();
        Comments comments = new Comments();
        for (int i = 0; i < commentsArray.size(); i++) {
            JsonObject element = commentsArray.get(i).getAsJsonObject();
            String id = element.get("id").getAsString();
            User user = parseUser(element.get("user").getAsJsonObject());
            long dateMilliSeconds = (long) element.get("timestamp").getAsFloat()*1000;
            Date date = new Date(dateMilliSeconds);

            Quest quest = parseQuestFromComments(element);
            Comment comment = new Comment();
            comment.setDate(date);
            comment.setId(id);
            comment.setQuest(quest);
            comment.setUser(user);
            comments.addComment(comment);
        }

        comments.sort();
        return comments;
    }
}
